package com.jhgsys.internal.hazard.controller;

import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.hazard.entity.HazardReportConfig;
import com.jhgsys.internal.hazard.service.IHazardReportConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 *  Controller
 *
 * @author Lujiachen
 * @date 2024-08-12 15:42:05
 */
@Slf4j
@Validated
@Controller
public class HazardReportConfigController extends BaseController {

    @Autowired
    private IHazardReportConfigService hazardReportConfigService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "hazardReportConfig")
    @RequiresPermissions("hazardReportConfig:view")
    public String hazardReportConfigIndex(Model model){
        model.addAttribute("tenantList", getTenantList());
        model.addAttribute("tenantId", getCurrentTenantId());
        model.addAttribute("hasHeadCompanyRole", hasHeadCompanyRole());
        if(StringUtils.isNotBlank(getCurrentTenantId())){
            HazardReportConfig hazardReportConfig = hazardReportConfigService.findHazardReportConfigByTenantId(getCurrentTenantId());
            if(hazardReportConfig!=null){
                model.addAttribute("hazardReportConfig",hazardReportConfig);
            }
        }
        return FebsUtil.view("hazard/hazardReportConfig/hazardReportConfigList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "hazardReportConfig/add")
    @RequiresPermissions("hazardReportConfig:add")
    public String hazardReportConfigAdd(){
        return FebsUtil.view("hazard/hazardReportConfig/hazardReportConfigEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "hazardReportConfig/update/{id}")
    @RequiresPermissions("hazardReportConfig:edit")
    public String hazardReportConfigUpdate(Model model, @PathVariable("id") String id){
        model.addAttribute("hazardReportConfig", hazardReportConfigService.findHazardReportConfigById(id));
        return FebsUtil.view("hazard/hazardReportConfig/hazardReportConfigEdit");
    }

    /**
     * 查询（所有）
     * @author Lujiachen
     * @date 2024-08-12 15:42:05
     */
    @GetMapping("hazardReportConfig")
    @ResponseBody
    @RequiresPermissions("hazardReportConfig:view")
    public FebsResponse getAllHazardReportConfigs(HazardReportConfig hazardReportConfig) {
        return new FebsResponse().success().data(hazardReportConfigService.findHazardReportConfigList(hazardReportConfig));
    }

    /**
     * 查询（分页）
     * @author Lujiachen
     * @date 2024-08-12 15:42:05
     */
    @GetMapping("hazardReportConfig/list")
    @ResponseBody
    @RequiresPermissions("hazardReportConfig:view")
    public FebsResponse hazardReportConfigList(QueryRequest request, HazardReportConfig hazardReportConfig) {
        Map<String, Object> dataTable = getDataTable(this.hazardReportConfigService.findHazardReportConfigPage(request, hazardReportConfig));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * 新增 HazardReportConfig
     * @author Lujiachen
     * @date 2024-08-12 15:42:05
     */
    @Log("新增HazardReportConfig")
    @PostMapping("hazardReportConfig")
    @ResponseBody
    @RequiresPermissions("hazardReportConfig:edit")
    public FebsResponse addHazardReportConfig(@RequestBody HazardReportConfig hazardReportConfig) throws FebsException {
        try {
            hazardReportConfig.setCreatedBy(getCurrentUser().getUserName());
            this.hazardReportConfigService.createHazardReportConfig(hazardReportConfig);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增HazardReportConfig失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
     * 逻辑删除 HazardReportConfig
     * @author Lujiachen
     * @date 2024-08-12 15:42:05
     */
    @Log("逻辑删除HazardReportConfig")
    @GetMapping("hazardReportConfig/delete/{id}")
    @ResponseBody
    @RequiresPermissions("hazardReportConfig:delete")
    public FebsResponse deleteHazardReportConfig(@PathVariable("id") String hazardReportConfigIds) throws FebsException {
        try {
            HazardReportConfig hazardReportConfig = new HazardReportConfig();
            hazardReportConfig.setDeletedBy(getCurrentUser().getUserName());
            hazardReportConfig.setDeletedTime(new Date());
            hazardReportConfig.setDeletedFlg(FebsConstant.INVALID);
            this.hazardReportConfigService.deleteHazardReportConfig(hazardReportConfig,hazardReportConfigIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除HazardReportConfig失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
    * 物理删除 HazardReportConfig
    * @author Lujiachen
    * @date 2024-08-12 15:42:05
    */
    @Log("物理删除HazardReportConfig")
    @GetMapping("hazardReportConfig/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("hazardReportConfig:delete")
    public FebsResponse physicallyDeleteHazardReportConfig(@PathVariable("id") String hazardReportConfigIds) throws FebsException {
        try {
            this.hazardReportConfigService.physicallyDeleteHazardReportConfig(hazardReportConfigIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除HazardReportConfig失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
     * 修改 HazardReportConfig
     * @author Lujiachen
     * @date 2024-08-12 15:42:05
     */
    @Log("修改HazardReportConfig")
    @PostMapping("hazardReportConfig/update")
    @ResponseBody
    @RequiresPermissions("hazardReportConfig:edit")
    public FebsResponse updateHazardReportConfig(@RequestBody HazardReportConfig hazardReportConfig) throws FebsException {
        try {
            hazardReportConfig.setUpdatedBy(getCurrentUser().getUserName());
            this.hazardReportConfigService.updateHazardReportConfig(hazardReportConfig);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改HazardReportConfig失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
     * 查询（单个）
     * @author Lujiachen
     * @date 2024-08-12 09:56:23
     */
    @GetMapping("hazardReportConfig/search/{id}")
    @ResponseBody
    @RequiresPermissions("hazardReportConfig:view")
    public FebsResponse getIncentiveRewardConfigById(Model model, @PathVariable("id") String  id) {
        HazardReportConfig hazardReportConfig = hazardReportConfigService.findHazardReportConfigByTenantId(id);
        model.addAttribute("hazardReportConfig", hazardReportConfig);
        return new FebsResponse().success().data(model);
    }

}
